package model

import (
	"gitee.com/carollia/data-sdk-go/utils"
	"errors"
)

// 物品
type Item struct {
	Type       string                 `json:"type"`
	Time       int64                  `json:"time"`
	ItemId     string                 `json:"item_id"`
	ItemType   string                 `json:"item_type"`
	Name       string                 `json:"name"`
	Price      float32                `json:"price"`
	Properties map[string]interface{} `json:"properties"`
}

func (item *Item) Normalize() error {
	if item.Type != "item_set" && item.Type != "item_delete" {
		return errors.New("property [Type] is not item_set or item_delete")
	}
	item.Time = utils.GetCNTimeNow()
	// check uid
	if item.ItemId == "" || len(item.ItemId) == 0 {
		return errors.New("property [ItemId] can't be empty")
	}
	if len(item.ItemId) > 255 {
		return errors.New("the max length of property [ItemId] is 255")
	}

	if item.Properties == nil {
		item.Properties = map[string]interface{}{}
	}
	return nil
}
